package com.dau.service.system.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dau.common.constants.CommConstants;
import com.dau.common.page.PageQuery;
import com.dau.common.page.TableDataInfo;
import com.dau.common.result.R;
import com.dau.entity.system.user.SysUser;
import com.dau.mapper.mapper.user.SysUserMapper;
import com.dau.service.system.user.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.Map;

/**
 * 用户服务impl
 *
 * @author tzj
 * @date 2024/11/07
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    /**
     * 分页查询用户
     *
     * @param sysUser   系统用户
     * @param pageQuery 分页参数
     * @return {@link TableDataInfo }<{@link SysUser }>
     */
    @Override
    public TableDataInfo<SysUser> selectUserPage(SysUser sysUser, PageQuery pageQuery) {
        Page<SysUser> page = baseMapper.selectUserPage(pageQuery.build(), this.buildQueryWrapper(sysUser));
        return TableDataInfo.build(page);
    }

    /**
     * 添加或更新
     *
     * @param sysUser 系统用户
     * @param userId  用户id
     * @return {@link R }<{@link SysUser }>
     */
    @Override
    public SysUser addOrUpdate(SysUser sysUser, Long userId) {
        String userIdStr = String.valueOf(userId);
        if (ObjectUtil.isNotNull(sysUser.getId())) {
            sysUser.setUpdateUser(userIdStr);
            sysUser.setUpdateTime(new Date());
            this.updateById(sysUser);
        } else {
            sysUser.setDelFlag(CommConstants.ONE);
            sysUser.setStatus(CommConstants.ONE);
            sysUser.setCreateUser(userIdStr);
            sysUser.setCreateTime(Date.from(Instant.now()));
            this.save(sysUser);
        }
        return sysUser;
    }

    /**
     * 查询用户信息(含用户所有部门)
     *
     * @param userId 用户id
     * @return {@link SysUser }
     */
    @Override
    public SysUser selectUserInfo(Long userId) {
        SysUser sysUser = baseMapper.selectById(userId);
        if (ObjectUtil.isNotNull(sysUser)) {
            sysUser.setDeptList(baseMapper.selectDeptListByUserId(userId));
        }
        return sysUser;
    }

    private Wrapper<SysUser> buildQueryWrapper(SysUser user) {
        Map<String, Object> params = user.getParams();
        QueryWrapper<SysUser> wrapper = Wrappers.query();
        wrapper.eq("del_flag", CommConstants.ONE)
                .eq(ObjectUtil.isNotNull(user.getId()), "id", user.getId())
                .like(StringUtils.isNotBlank(user.getUserName()), "user_name", user.getUserName())
                .eq(ObjectUtil.isNotNull(user.getStatus()), "status", user.getStatus())
                .like(StringUtils.isNotBlank(user.getPhone()), "phone", user.getPhone())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        "u.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
